package com.urea.system.config;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.urea.common.core.domain.entity.Customer;
import com.urea.common.core.domain.entity.SysUser;
import com.urea.common.core.redis.RedisCache;
import com.urea.common.exception.ServiceException;
import com.urea.common.utils.StringUtils;
import com.urea.common.utils.spring.SpringUtils;
import com.urea.system.constants.BusinessConstants;
import com.urea.system.domain.*;
import com.urea.system.domain.vo.OrderVo;
import com.urea.system.domain.vo.PayVo;
import com.urea.system.service.*;
import com.urea.system.service.impl.*;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import net.bytebuddy.asm.Advice;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttCallbackExtended;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @Author: liuzd
 * @Date: 2023/7/30 17:06
 * @Description:
 */
@Slf4j
@Component
public class MqttConsumerCallBack implements MqttCallbackExtended {

    @Autowired
    private MqttConsumer consumer;
    /**
     * 客户端断开连接的回调
     * @author liuzd
     * @param throwable
     * @return void
     * @date 2023/7/30 17:14
     */
    @Override
    public void connectionLost(Throwable throwable) {
        log.info("################consumer与服务器断开连接,尝试重新连接,异常原因:{}##########################",throwable.getCause().getMessage());
//        throwable.printStackTrace();
//        log.info("################异常原因:{}##########################",JSONObject.from(throwable).toJSONString());
//        consumer = SpringUtils.getBean("mqttConsumer");
//        consumer.connect();
    }

    /**
     * 消息到达的回调
     * @author liuzd
     * @param topic
     * @param message
     * @return void
     * @date 2023/7/30 17:14
     */
    @Override
    public void messageArrived(String topic, MqttMessage message){
        consumer = SpringUtils.getBean("mqttConsumer");
        consumer.messageArrived(topic,message);

    }

    /**
     * 消息发布成功的回调
     * @author liuzd
     * @param iMqttDeliveryToken
     * @return void
     * @date 2023/7/30 17:14
     */
    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
        log.info("############消息发送成功############");
    }



    @Override
    public void connectComplete(boolean b, String s) {
        log.info("############消息服务器连接成功############");
        consumer = SpringUtils.getBean("mqttConsumer");
        consumer.subscribe();
    }


}